<%@ page language='java' contentType='text/html; charset=utf-8'
	pageEncoding='utf-8'%>
<%@ include file='../common/base.jsp'%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>

<meta http-equiv='X-UA-Compatible' content='IE=10' />
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
<title>部门管理系统</title>
<link href='${path }/dist/css/bootstrap.min.css' rel='stylesheet' />
<link href='${path }/dist/css/Buttom.css' rel='stylesheet' />
<link href='${path }/dist/css/demo.css' rel='stylesheet' />
<link href='${path }/dist/css/zTreeStyle/zTreeStyle.css' rel='stylesheet' />

</head>
<body>

	<div id='treeDemo' class="ztree"></div>
	<div id='treeDemo2' class="ztree"></div>
	<button type="button" id="test">111</button>

	<script src='${path }/dist/js/jquery.min.js'></script>
	<script src='${path }/dist/js/bootstrap.min.js'></script>
	<script src='${path }/dist/js/jquery.ztree.core-3.5.js'></script>
<SCRIPT type="text/javascript">
		
		var setting = {
			view: {
				selectedMulti: false
			},
			async: {
				enable: true,
				url:"${path}/jsontest",
				autoParam:["id"],
				otherParam:{"otherParam":"zTreeAsyncTest"},
				dataFilter: filter
			},
			callback: {
				beforeClick: beforeClick,
				beforeAsync: beforeAsync,
				onAsyncError: onAsyncError,
				onAsyncSuccess: onAsyncSuccess
			},
			data:{
				simpleData:{
					enable: true,
				}
			}
		};

		function filter(treeId, parentNode, childNodes) {
			if (!childNodes) return null;
			for (var i=0, l=childNodes.length; i<l; i++) {
				childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
			}
			return childNodes;
		}
		function beforeClick(treeId, treeNode) {
			if (!treeNode.isParent) {
				//alert("请选择父节点");
				return true;
			} else {
				return true;
			}
		}
		var log, className = "dark";
		function beforeAsync(treeId, treeNode) {
			className = (className === "dark" ? "":"dark");
			showLog("[ "+getTime()+" beforeAsync ]&nbsp;&nbsp;&nbsp;&nbsp;" + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
			return true;
		}
		function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
			showLog("[ "+getTime()+" onAsyncError ]&nbsp;&nbsp;&nbsp;&nbsp;" + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
		}
		function onAsyncSuccess(event, treeId, treeNode, msg) {
			showLog("[ "+getTime()+" onAsyncSuccess ]&nbsp;&nbsp;&nbsp;&nbsp;" + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
		}
		
		function showLog(str) {
			if (!log) log = $("#log");
			log.append("<li class='"+className+"'>"+str+"</li>");
			if(log.children("li").length > 8) {
				log.get(0).removeChild(log.children("li")[0]);
			}
		}
		function getTime() {
			var now= new Date(),
			h=now.getHours(),
			m=now.getMinutes(),
			s=now.getSeconds(),
			ms=now.getMilliseconds();
			return (h+":"+m+":"+s+ " " +ms);
		}
		function refreshNode(e) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo2"),
			type = e.data.type,
			silent = e.data.silent,
			nodes = zTree.getSelectedNodes();
			if (nodes.length == 0) {
				alert("请先选择一个父节点");
			}
			
			for (var i=0, l=nodes.length; i<l; i++) {
				//alert("length="+l+"[]="+nodes[i].id);
				zTree.reAsyncChildNodes(nodes[i], type, silent);
				if (!silent) zTree.selectNode(nodes[i]);
			}
		}
		
		$(document).ready(function(){
			
			$.fn.zTree.init($("#treeDemo2"), setting);
			$("#test").bind("click",{type:"refresh", silent:true},refreshNode);
		});
		  
	</SCRIPT>

</body>
</html>